#!/bin/sh
# Copyright (C) 2006-2008,  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

#
# For more information visit http://carrierclass.net/
#

SOURCE=.
TARGET=

# ---------------------------
etc_ccn="${TARGET}/etc/ccn"
TARGET_CONFIG="${etc_ccn}/config"

if [ -e "${TARGET_CONFIG}" ]
then
    echo "Using existing ${TARGET_CONFIG}"
    TARGET_CONFIG="${TARGET_CONFIG}.orig"
else
    echo "Installing ${TARGET_CONFIG}"
fi

mkdir -p "${etc_ccn}"
perl -p -e 'BEGIN { $n = shift } s/TARGET/$n/' "${TARGET}" \
    < "${SOURCE}/layout/etc/ccn/config" > "${TARGET_CONFIG}"

# ---------------------------
echo -n "Installing /usr/lib/ccn"
usr_lib_ccn=`perl -n -e 'print $1 if /^usr_lib_ccn\s+(\S+)/' ${etc_ccn}/config`
if [ "x${usr_lib_ccn}" == "x" ]; then echo ": no config"; exit 1; fi
echo " into ${usr_lib_ccn}"

mkdir -p "${usr_lib_ccn}"
cp -R "${SOURCE}/layout/usr/lib/ccn"/* "${usr_lib_ccn}"

# ---------------------------
echo -n "Installing web content"
www_root=`perl -n -e 'print $1 if /^www_root\s+(\S+)/' ${etc_ccn}/config`
if [ "x${www_root}" == "x" ]; then echo ": no config"; exit 1; fi
WWW_USER=`perl -n -e 'print $1 if /^www_user\s+(\S+)/' ${etc_ccn}/config`
echo " into ${www_root}/js/ccn"

mkdir -p "${www_root}/js/ccn"
cp "${SOURCE}/layout/usr/lib/ccn/www/js/ccn"/* "${www_root}/js/ccn"

mkdir -p "${www_root}/ccn_icons"
cp "${SOURCE}/layout/usr/lib/ccn/www/ccn_icons"/* "${www_root}/ccn_icons"

# We mark the directories as non-readable, but make
# sure to disable directory listing in the web server configuration!!

mkdir -p "${www_root}/json"
chown ${WWW_USER} "${www_root}/json"
chmod og-r "${www_root}/json"

mkdir -p "${www_root}/themes"
chown ${WWW_USER} "${www_root}/themes"
chmod og-r "${www_root}/themes"

mkdir -p "${www_root}/upload"
chown ${WWW_USER} "${www_root}/upload"
chmod og-r "${www_root}/upload"

# ---------------------------
echo -n "Installing /var/spool/ccn"
var_spool_ccn=`perl -n -e 'print $1 if /^var_spool_ccn\s+(\S+)/' ${etc_ccn}/config`
WWW_USER=`perl -n -e 'print $1 if /^www_user\s+(\S+)/' ${etc_ccn}/config`
echo " into ${var_spool_ccn} for ${WWW_USER}"

mkdir -p "${var_spool_ccn}"
chown ${WWW_USER} "${var_spool_ccn}/"

mkdir -p "${var_spool_ccn}/config"
chown ${WWW_USER} "${var_spool_ccn}/config/"

for db in configuration auth; do {
    if [ ! -e "${var_spool_ccn}/$db.sqlite" ]; then {
        echo "Creating $db database";
        sqlite3 "${var_spool_ccn}/$db.sqlite" \
            < "${SOURCE}/layout/usr/lib/ccn/sql/$db.sql";
    } fi
    chown ${WWW_USER} "${var_spool_ccn}/$db.sqlite";
} done

# ---------------------------
echo -n "Installing web script"
www_script=`perl -n -e 'print $1 if /^www_script\s+(\S+)/' ${etc_ccn}/config`
echo " into ${www_script}"

perl -p -e 'BEGIN { $n = shift } s/USR_LIB_CCN/$n/' "${usr_lib_ccn}" \
    < "${SOURCE}/layout/usr/lib/ccn/www/index.pl" > "${www_script}"
chmod +x "${www_script}"

# -----------------------------
echo -n "Installing sbin scripts"
echo " into ${usr_lib_ccn}/sbin"

mkdir -p "${usr_lib_ccn}/sbin"
(
    cd "${SOURCE}/layout/usr/lib/ccn/sbin"
    for f in *; do
        cat "$f" |
        perl -p -e 'BEGIN { $n = shift } s/USR_LIB_CCN/$n/' "${usr_lib_ccn}" \
        > "${usr_lib_ccn}/sbin/$f"
    done
)
chmod +x "${usr_lib_ccn}/sbin"/*

# -----------------------------
echo -n "Installing bin scripts"
echo " into ${usr_lib_ccn}/bin"

mkdir -p "${usr_lib_ccn}/bin"
(
    cd "${SOURCE}/layout/usr/lib/ccn/bin"
    for f in *; do
        cat "$f" |
        perl -p -e 'BEGIN { $n = shift } s/USR_LIB_CCN/$n/' "${usr_lib_ccn}" \
        > "${usr_lib_ccn}/bin/$f"
    done
)
chmod +x "${usr_lib_ccn}/bin"/*

